package com.lenovo.leos.filebrowser.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;

import com.lenovo.leos.filebrowser.R;



public class FileDialog {
	private static final String TAG = "FileDialog";
	
	public static final int INVALID_RID = 0;
	
	private static DisplayMetrics sdisplayMetric = null;
	private static Activity sActivity = null;
			
	public static final DialogButton DEFAULT_BUTTON_OK = new DialogButton(R.string.dlg_btn_ok, null);
	public static final DialogButton DEFAULT_BUTTON_CANCEL = new DialogButton(R.string.dlg_btn_cancel, null);
		
	public static class DialogButton extends Object {
		private static final String TAG = "DialogButton";

		private int miRid = 0;
		private DialogInterface.OnClickListener mcListener = null;

		public DialogButton(int rid, DialogInterface.OnClickListener listener) {
			miRid = rid;
			mcListener = listener;
		}
		
		public int getRid(){
			return miRid;
		}
		
		public DialogInterface.OnClickListener getListener() {
			return mcListener;
		}
	}

	// 0 for rid means no this item
	// [improve] dismiss listener
	private static AlertDialog.Builder showMsgInternal(Context context,
			int titleRid,
			int msgRid,
			View content,
			DialogButton dbp,
			DialogButton dbm,
			DialogButton dbn
			) {
		
		if (null == context){
			Log.e(TAG, "context is null in showMsg");
			return null;
		}else {
			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			
			if (0 < titleRid) 
				builder = builder.setTitle(titleRid);
			
			if (0 < msgRid)
				builder = builder.setMessage(msgRid);
			
			if (null != content)
				builder = builder.setView(content);
			
			if (null != dbp)
				builder = builder.setPositiveButton(dbp.getRid(), dbp.getListener());
			
			if (null != dbm)
				builder = builder.setNeutralButton(dbm.getRid(), dbm.getListener());
			
			if (null != dbn)
				builder = builder.setNegativeButton(dbn.getRid(), dbn.getListener());
			
			builder.show();
			return builder;
		}		
	}
	
	public static AlertDialog.Builder showMsgWithTwoButton(Context context,
			int titleRid,
			int msgRid,
			DialogButton dbp,
			DialogButton dbn
			){
		Log.i(TAG, "==> showMsgWithTwoButton");
		return showMsgInternal(context, titleRid, msgRid, null, dbp, null, dbn);
	}
	
	public static AlertDialog.Builder showMsgWithOneButton(Context context, 
			int titleRid, 
			int msgRid, 
			DialogButton dbp) {
		Log.i(TAG, "==> showMsgWithOneButton");
		return showMsgInternal(context, titleRid, msgRid, null, dbp, null, null);
	}
	
	public static AlertDialog.Builder showMsgWithThreeButton(Context context, 
			int titleRid, 
			int msgRid, 
			DialogButton dbp,			
			DialogButton dbm,
			DialogButton dbn) {
		Log.i(TAG, "==> showMsgWithThreeButton");
		return showMsgInternal(context, titleRid, msgRid, null, dbp, dbm, dbn);
	}
	
	public static AlertDialog.Builder showMsg(Context context,
			int titleRid,
			int msgRid,
			View content,
			DialogButton dbp,
			DialogButton dbm,
			DialogButton dbn
			) {
		return showMsgInternal(context, titleRid, msgRid, content, dbp, dbm, dbn);				
	}
	
	
	/*
	 * [popup dialog]
	 * comment: below functions should be used to show a popup 
	 * dialog which we can decide whether it will be dismissed 
	 * after pressing the button. 
	 */
	
//	private static OnPopDlgButtonListener okListener = null;
//	private static OnPopDlgButtonListener cancelListener = null;
//	
//	public interface OnPopDlgButtonListener {
//		// return true ==> dismiss the dialog
//		// return false ==> the dailog still show
//		public boolean onClick(View v);
//	}
//	
//	private static final OnClickListener POPUP_DIALOG_OK_LISTENER = new OnClickListener() {
////		@Override
//		public void onClick(View v) {
//			// TODO Auto-generated method stub
//			boolean res = true;
//			
//			if(null != okListener){
//				res = okListener.onClick(v);
//			}
//			
//			if(res)
//				spmDialog.dismissMode();
//		}		
//	};
//	
//	private static final OnClickListener POPUP_DIALOG_CANCEL_LISTENER = new OnClickListener() {
////		@Override
//		public void onClick(View v) {
//			// TODO Auto-generated method stub
//			boolean res = true;
//			
//			if(null != cancelListener){
//				res = cancelListener.onClick(v);
//			}
//			
//			if(res)
//				spmDialog.dismissMode();			
//		}		
//	};
	
//	public static void showInputDialog(Activity activity,
//			View content,
//			int titleRid,
//			OnPopDlgButtonListener okListener,
//			OnPopDlgButtonListener cancelListener){
//		
//		if (null == sdisplayMetric) {
//			sdisplayMetric = new DisplayMetrics();
//			activity.getWindowManager().getDefaultDisplay().getMetrics(sdisplayMetric);
//		}
//		
//		if (null == spmDialog){
//			spmDialog = new PopupMenu(activity, 360, 300, true, true);
//		}else {
//			if (!activity.equals(sActivity)){
//				sActivity = activity;
//				spmDialog = new PopupMenu(sActivity, 360, 300, true, true);
//			}			
//		}
//		
//		spmDialog.setUserContentView(content);
//		spmDialog.setHasDividerTop(true);
//		spmDialog.setHasDividerFoot(true);
//		spmDialog.setIsMode(true, true);
//		spmDialog.setHasDefaultTopView(true, TOPVIEW_STYLE.DIALOG_STYLE);
//		spmDialog.setHasDefaultFootView(true, FOOTVIEW_STYLE.TWO_BUTTON);
//		
//		spmDialog.getDefaultOkButton().setOnClickListener(POPUP_DIALOG_OK_LISTENER);
//		spmDialog.getDefaultCancelButton().setOnClickListener(POPUP_DIALOG_CANCEL_LISTENER);
//		
//		spmDialog.showMenu((sdisplayMetric.widthPixels - 360) / 2, 
//				(sdisplayMetric.heightPixels - 300) / 2, 
//				Gravity.NO_GRAVITY);				
//	}
	
//	public static void showInputDialog(final Activity activity,
//			int msgRid,
//			int defRid,
//			int titleRid,
//			final File curDir)
//	{
//		// content view
//		LayoutInflater inflater = LayoutInflater.from(activity);
//		View content = inflater.inflate(R.layout.dlg_content_newname, null);
//		TextView tv = (TextView) content.findViewById(R.id.filename_view);
//		tv.setText(msgRid);
//		final EditText et = (EditText) content.findViewById(R.id.filename_edit);
//
//		et.setText(defRid);
//		et.setFilters(new InputFilter[] { new InputFilter.LengthFilter(
//						Constant.FILE_NAME_NUM_MAX - 4) });
//		et.selectAll();	
//		
//		// dialog
//		new AlertDialog.Builder(activity)
//		.setTitle(titleRid)
//		.setView(content)
//		.setPositiveButton(R.string.alert_dialog_ok,
//						new DialogInterface.OnClickListener() {
//							public void onClick(DialogInterface dialog,
//									int whichButton) {
//								StringBuilder name_doc;
//								String name = et.getText()
//										.toString();
//								name_doc = new StringBuilder();
//								name = name_doc.append(name).append(
//										".doc").toString();
//								if (FileUtil
//										.getFileNameWithoutExt(name)
//										.length() > 0
//										&& name.length() <= Constant.FILE_NAME_NUM_MAX) {
//
//									if (FileUtil.isNameUseable(name,
//											true) == false) {
//										Toast.makeText(
//												activity,
//												R.string.namebad,
//												Toast.LENGTH_SHORT)
//												.show();
//									} else {
//										File f = new File(curDir.getAbsolutePath()
//												+ "/" + name);
//										if (f.exists() == true
//												&& f.isFile() == true) {
//											FileDialog
//													.showMsgWithOneButton(
//															activity,
//															R.string.dlg_error_title,
//															R.string.dlg_error_msg_existed_file,
//															FileDialog.DEFAULT_BUTTON_OK);
//										} else {
//											// creatDir(name);
////											createOffice(name,NEW_FILE_DOC);
//											// Toast.makeText(FileBrowser.this,
//											// R.string.docOk,
//											// Toast.LENGTH_SHORT).show();
//										}
//									}
//
//								} else if (name.length() > Constant.FILE_NAME_NUM_MAX) {
//									Toast.makeText(activity,
//											R.string.name_too_long,
//											Toast.LENGTH_SHORT).show();
//								} else {
//									Toast.makeText(activity,
//											R.string.nameempty,
//											Toast.LENGTH_SHORT).show();
//								}
//							}
//						}).setNegativeButton(
//						R.string.alert_dialog_cancel,
//						new DialogInterface.OnClickListener() {
//							public void onClick(DialogInterface dialog,
//									int whichButton) {
//								// User clicked cancel
//								// so do some stuff
//							}
//						}).show();
//	}
}